package cn.iocoder.yudao.module.system.service.workrack;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.workorder.WorkOrderDO;
import cn.iocoder.yudao.module.system.dal.mysql.workorder.WorkOrderMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.workrack.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workrack.WorkRackDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.workrack.WorkRackMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 上挂记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class WorkRackServiceImpl implements WorkRackService {

    @Resource
    private WorkOrderMapper workOrderMapper;
    @Resource
    private WorkRackMapper workRackMapper;

    @Override
    public Integer createWorkRack(WorkRackSaveReqVO createReqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        //更新订单信息
        UpdateWrapper<WorkOrderDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",createReqVO.getWorkId());
        updateWrapper.setSql("rack_uid="+userId+",white_items_num = white_items_num - "+createReqVO.getHangNum()+",racking_quantity = racking_quantity + "+createReqVO.getHangNum());
        workOrderMapper.update(updateWrapper);
        WorkOrderDO workOrderDO = workOrderMapper.selectById(createReqVO.getWorkId());

        // 插入
        WorkRackDO workRack = BeanUtils.toBean(createReqVO, WorkRackDO.class);
        workRack.setRemainNum(workOrderDO.getWhiteItemsNum());
        workRack.setCustomerId(workOrderDO.getCustomerId());
        workRackMapper.insert(workRack);
        // 返回
        return workRack.getId();
    }

    @Override
    public void updateWorkRack(WorkRackSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkRackExists(updateReqVO.getId());
        // 更新
        WorkRackDO updateObj = BeanUtils.toBean(updateReqVO, WorkRackDO.class);
        workRackMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkRack(Integer id) {
        // 校验存在
        validateWorkRackExists(id);
        // 删除
        workRackMapper.deleteById(id);
    }

    @Override
        public void deleteWorkRackListByIds(List<Integer> ids) {
        // 校验存在
        validateWorkRackExists(ids);
        // 删除
        workRackMapper.deleteByIds(ids);
        }

    private void validateWorkRackExists(List<Integer> ids) {
        List<WorkRackDO> list = workRackMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WORK_RACK_NOT_EXISTS);
        }
    }

    private void validateWorkRackExists(Integer id) {
        if (workRackMapper.selectById(id) == null) {
            throw exception(WORK_RACK_NOT_EXISTS);
        }
    }

    @Override
    public WorkRackDO getWorkRack(Integer id) {
        return workRackMapper.selectById(id);
    }

    @Override
    public PageResult<WorkRackDO> getWorkRackPage(WorkRackPageReqVO pageReqVO) {
        return workRackMapper.selectPage(pageReqVO);
    }

}
